package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.DModuleDetails;
import com.guigu.www.pojo.*;
import com.guigu.www.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.util.*;

@RestController
public class DModuleController {
    //产品物料组成
    @Autowired
    ModuleService moduleService;

    //产品物料组成明细
    @Autowired
    DModuleDetailsService dModuleDetailsService;

    //产品生产工序物料明细
    @Autowired
    MDesingProndureModuleService mDesingProndureModuleService;

    //产品生产工序明细表
    @Autowired
    MDesignProcedureDetailsService mdpdservice2;

    //产品生产工序
    @Autowired
    MDesignProcedureService mDesignProcedureService;

    //SELECT id FROM D_MODULE WHERE PRODUCT_ID='100010101000001'
    //设计按钮
    //*
    @RequestMapping("querydModuleDetailsSbycpid.action")
    public PageVo<MDesingProndureModule> querydModuleDetailsSbycpid(                                                                     String productid,Integer id,
                                                                    @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                                    @RequestParam(value = "pagesize", defaultValue = "5") int pagesize, Map map) {
        //id
        QueryWrapper<DesignProcedure> queryWrapper4 = new QueryWrapper<DesignProcedure>();
        queryWrapper4.eq("PRODUCT_ID", productid);
        DesignProcedure d4 = mDesignProcedureService.getOne(queryWrapper4);

        //id
        QueryWrapper<DesignProcedureDetails> queryWrapper5 = new QueryWrapper<DesignProcedureDetails>();
        queryWrapper5.eq("PARENT_ID",d4.getId());
        List<DesignProcedureDetails> d5= mdpdservice2.list(queryWrapper5);

        QueryWrapper<Module> queryWrapper2 = new QueryWrapper<Module>();
        queryWrapper2.eq("PRODUCT_ID", productid);
        Module d = moduleService.getOne(queryWrapper2);
        QueryWrapper<ModuleDetails> queryWrapper = new QueryWrapper<ModuleDetails>();
        queryWrapper.eq("PARENT_ID", d.getId());
        List<ModuleDetails> list = dModuleDetailsService.list(queryWrapper);
        for (ModuleDetails details : list) {
            System.out.println("wowoo"+details);
            MDesingProndureModule m = new MDesingProndureModule();
            m.setProductId(details.getProductid());
            m.setParentid(id);
            m.setProductName(details.getProductname());
            m.setDetailsNumber(1);
            //detailsNumber
            m.setType(details.getType());
            m.setAmount(details.getAmount());
            m.setProductDescribe(details.getProductdescribe());
            m.setAmountUnil(details.getAmountunit());
            m.setCostPrice(details.getCostprice());
            m.setSubtotal(details.getSubtotal());
            System.out.println(m);
            mDesingProndureModuleService.save(m);
        }
        //d5
        QueryWrapper<MDesingProndureModule> queryWrapper6 = new QueryWrapper<MDesingProndureModule>();
        queryWrapper6.eq("PARENT_ID", d5.get(0).getId());

        PageVo<MDesingProndureModule> pageVo = new PageVo<MDesingProndureModule>();
        Page<MDesingProndureModule> page = mDesingProndureModuleService.page(new Page<MDesingProndureModule>(pagenum, pagesize),queryWrapper6);
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }


    //*c
    @RequestMapping("querydModuleDetailsSbycpid22.action")//!
    public PageVo<ModuleDetails> querydModuleDetailsSbycpid22(String productid,
                                                          @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                          @RequestParam(value = "pagesize", defaultValue = "5") int pagesize,Map map){

        QueryWrapper<Module> queryWrapper2 = new QueryWrapper<Module>();
        queryWrapper2.eq("PRODUCT_ID",productid);
        Module d=moduleService.getOne(queryWrapper2);
        System.out.println(d);

        QueryWrapper<ModuleDetails> queryWrapper = new QueryWrapper<ModuleDetails>();
        queryWrapper.eq("PARENT_ID",d.getId());

        PageVo<ModuleDetails> pageVo = new PageVo<ModuleDetails>();

        Page<ModuleDetails> page =dModuleDetailsService.page(new Page<ModuleDetails>(pagenum, pagesize),queryWrapper);

        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


    //*
    @RequestMapping("querydDesignProcedureDetailscpid22.action")//!2
    public PageVo<DesignProcedureDetails> querydDesignProcedureDetailscpid22(String productid,
                                                          @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                          @RequestParam(value = "pagesize", defaultValue = "5") int pagesize,Map map){

        QueryWrapper<DesignProcedure> queryWrapper2 = new QueryWrapper<DesignProcedure>();
        queryWrapper2.eq("PRODUCT_ID",productid);
        DesignProcedure d=mDesignProcedureService.getOne(queryWrapper2);
        System.out.println(d);

        QueryWrapper<DesignProcedureDetails> queryWrapper = new QueryWrapper<DesignProcedureDetails>();
        queryWrapper.eq("PARENT_ID",d.getId());

        PageVo<DesignProcedureDetails> pageVo = new PageVo<DesignProcedureDetails>();

        Page<DesignProcedureDetails> page =mdpdservice2.page(new Page<DesignProcedureDetails>(pagenum, pagesize),queryWrapper);

        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    //*
    @RequestMapping("querydDesignProcedureDetailscpid22Id.action")//!2
    public PageVo<DesignProcedureDetails> querydDesignProcedureDetailscpid22Id(String productId,
                                                          @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                          @RequestParam(value = "pagesize", defaultValue = "5") int pagesize,Map map){

        QueryWrapper<DesignProcedure> queryWrapper2 = new QueryWrapper<DesignProcedure>();
        queryWrapper2.eq("PRODUCT_ID",productId);
        DesignProcedure d=mDesignProcedureService.getOne(queryWrapper2);
        System.out.println(d);

        QueryWrapper<DesignProcedureDetails> queryWrapper = new QueryWrapper<DesignProcedureDetails>();
        queryWrapper.eq("PARENT_ID",d.getId());

        PageVo<DesignProcedureDetails> pageVo = new PageVo<DesignProcedureDetails>();

        Page<DesignProcedureDetails> page =mdpdservice2.page(new Page<DesignProcedureDetails>(pagenum, pagesize),queryWrapper);

        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


    //确定按钮
    @RequestMapping("updateDModulesj.action")
    public Map updateDModulesj(@RequestBody DesignProcedureDetails designProcedureDetails
    ) {
        Map map = new HashMap();
        //当前工序物料标志
        designProcedureDetails.setDesignmoduletag("D002-1");
        designProcedureDetails.setModulesubtotal(designProcedureDetails.getModulesubtotal());
        boolean f = mdpdservice2.updateById(designProcedureDetails);
        List<MDesingProndureModule> list = designProcedureDetails.getDetailss();
        for (MDesingProndureModule details : list) {
            UpdateWrapper<MDesingProndureModule> updateWrapper=new  UpdateWrapper<MDesingProndureModule>();
            updateWrapper.set("AMOUNT",details.getAmount())
                    .set("SUBTOTAL",details.getSubtotal())
                    .eq("PARENT_ID",designProcedureDetails.getId());
            mDesingProndureModuleService.update(null,updateWrapper);
        }
        if (f) {
            map.put("msg", "设计成功");
            map.put("cm", "1");
        } else {
            map.put("msg", "设计失败");
        }
        return map;
    }


    //确定按钮update变更
    @RequestMapping("updateDModulesjupdatebiangeng.action")
    public Map updateDModulesjupdatebiangeng(@RequestBody DesignProcedureDetails designProcedureDetails
    ) {
        Map map = new HashMap();
        //当前工序物料变更标志
        designProcedureDetails.setDesignmodulechangetag("D003-1");
        //审核
        designProcedureDetails.setDesignmodulechangetag("G002-1");
        boolean f = mdpdservice2.updateById(designProcedureDetails);
        List<MDesingProndureModule> list = designProcedureDetails.getDetailss();
        for (MDesingProndureModule details : list) {
            UpdateWrapper<MDesingProndureModule> updateWrapper=new  UpdateWrapper<MDesingProndureModule>();
            updateWrapper.set("AMOUNT",details.getAmount())
                    .set("SUBTOTAL",details.getSubtotal())
                    .eq("PARENT_ID",designProcedureDetails.getId());
            mDesingProndureModuleService.update(null,updateWrapper);
        }
        if (f) {
            map.put("msg", "变更成功");
            map.put("cm", "1");
            //
        } else {
            map.put("msg", "变更失败");
        }
        return map;
    }


    //删除明细表
    @RequestMapping("delwlmx.action/{id}")
    public Map delwlmx(@PathVariable Integer id
    ) {
        Map map = new HashMap();
        boolean f= mDesingProndureModuleService.removeById(id);
        return map;
    }

    //可用数量
    @RequestMapping("updatemcDModulepojo.action")
    public void updatemcDModulepojo(@RequestBody ModuleDetails moduleDetails
    ) {
        UpdateWrapper<ModuleDetails> updateWrapper
                =new UpdateWrapper<ModuleDetails>();
        updateWrapper.set("RESIDUAL_AMOUNT",moduleDetails.getResidualamount())
                .eq("PRODUCT_ID",moduleDetails.getProductid());
       dModuleDetailsService.update(null,updateWrapper);
    }

    //*max已设计提交
    @RequestMapping("updategongxuMDesignProcedureDetails.action")
    public Map updateMDesignProcedureDetailsshow(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setDesignmoduletag("D002-1");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        if (f) {
            map.put("msg", "提交成功");
            map.put("cm", "1");

        } else {
            map.put("msg", "提交失败");
        }
        return map;
    }



    //更据父工序（物料表查询）
    @RequestMapping("queryMDesignProcedureDetailsControllerbyparentid.action/{parentid}")
    public PageVo<MDesingProndureModule> queryMDesignProcedureDetailsControllerbyparentid(
                                                                        @PathVariable Integer parentid,
                                                                        @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                                        @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        QueryWrapper<MDesingProndureModule> queryWrapper = new QueryWrapper<MDesingProndureModule>();
        DesignProcedureDetails d = mdpdservice2.getById(parentid);
        queryWrapper.eq("PARENT_ID", d.getId());

        Page<MDesingProndureModule> page =  mDesingProndureModuleService.page(new Page<MDesingProndureModule>(pagenum, pagesize), queryWrapper);
        PageVo<MDesingProndureModule> pageVo = new PageVo<MDesingProndureModule>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    //查看物料***
    @RequestMapping("querydesignProcedureDetailsquery2.action/{parentid}")
    public PageVo<MDesingProndureModule> querydesignProcedureDetailsquery2(
            @PathVariable Integer parentid,
            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
            @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        QueryWrapper<MDesingProndureModule> queryWrapper = new QueryWrapper<MDesingProndureModule>();
        DesignProcedureDetails d = mdpdservice2.getById(parentid);

        queryWrapper.eq("PARENT_ID", d.getId());

        Page<MDesingProndureModule> page =mDesingProndureModuleService.page(new Page<MDesingProndureModule>(pagenum, pagesize), queryWrapper);

        PageVo<MDesingProndureModule> pageVo = new PageVo<MDesingProndureModule>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }




    //审核 cant look not write (parent parent)
    @RequestMapping("updateDModulshow.action")
    public Map updateDModulshow(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setDesignmoduletag("G002-2");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        if (f) {
            map.put("msg", "审核成功");
        } else {
            map.put("msg", "审核失败");
        }
        return map;
    }




    //not审核
    @RequestMapping("notupdateDModulshow.action")
    public Map notupdateDModulshow(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setDesignmoduletag("G002-3");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        if (f) {
            map.put("msg", "复核不通过，请重新制定");
        } else {
            map.put("msg", "失败");
        }
        return map;
    }


    //变更物料工序明细











    //添加产品物料组成
    @RequestMapping("AddModuleAndBatchModuleDetails.action")
    public Map AddModuleAndBatchModuleDetails(@RequestBody Module module2, HttpSession session) {

        Map map = new HashMap();

        //获取登记人
        module2.setRegister(session.getAttribute("username").toString());

        //设置设计人
        module2.setDesigner(session.getAttribute("username").toString());

        Date date = new Date();

        //获取登记时间
        module2.setRegistertime(date);

        boolean b = moduleService.AddModuleAndBatchModuleDetails(module2);

        if (b) {
            map.put("msg", "添加成功");
        } else {
            map.put("msg", "添加失败");
        }

        return map;
    }

    //根据id查询
    @RequestMapping("QueryModuleById.action/{id}")
    public Module QueryModuleById(@PathVariable int id) {
        return moduleService.QueryModuleById(id);
    }


    //修改产品物料组成
    @RequestMapping("UpdateModule.action")
    public Map UpdateModule(@RequestBody Module module2, HttpSession session) {

        Map map = new HashMap();

        //获取更改人
        module2.setChanger(session.getAttribute("username").toString());

        //设置设计人
        module2.setDesigner(session.getAttribute("username").toString());

        Date date = new Date();

        //设置更改时间
        module2.setChangetime(date);

        System.out.println(module2);

        boolean b = moduleService.UpdateModuleByDesignId(module2);

        if (b) {
            map.put("msg", "修改成功");
        } else {
            map.put("msg", "修改失败");
        }

        return map;
    }

    //点击审核物料组成设计单
    @PutMapping("shenheModule.action")
    public Map shenheModule(@RequestBody Module obj) {
        obj.setChecktime(new Date());

        System.out.println(obj);
        Map map = new HashMap();
        boolean f = moduleService.shenheModule(obj);

        if (f) {
            map.put("msg", "操作成功");
        } else {
            map.put("msg", "操作失败");
        }
        return map;
    }
}
